﻿namespace DimensionsHelper.ExcelAddIn.Core.Sql;

/// <summary>
/// SQL字段码表的设置类
/// </summary>
public class CodeMapSettings : ExcelAddInSettingsBase
{

    public override string Name { get; set; } = string.Empty;

    /// <summary>
    /// 开始读取的行号，最小值是1，如果设置的值小于1，视为1.
    /// </summary>
    public int StartRow { get; set; } = 1;

    /// <summary>
    /// 码号列号，1开始，查找内容时，将从前向后依次查找，找到第一个非空值为止
    /// </summary>
    public int[] CodeColumns { get; set; } = [];

    /// <summary>
    /// 描述列号，1开始，查找内容时，将从前向后依次查找，找到第一个非空值为止
    /// </summary>
    public int[] LabelColumns { get; set; } = [];

    /// <summary>
    /// 分类码号级别的区分方式
    /// </summary>
    public CodeLevelMarkType LevelMarkType { get; set; }

    /// <summary>
    /// 分类码号级别的组织方式
    /// </summary>
    public CodeLevelOrderType OrderType { get; set; }

    /// <summary>
    /// 分类码号的颜色级别定义，数组的第一维度表示不同的分类级别，索引越小越靠近上层。
    /// 第二个维度表示当前级别所有匹配的颜色。匹配时，第一个维度从0开始依次向后进行查找，
    /// 直到查找到第一个符合的颜色。
    /// </summary>
    public int[][] ColorLevels { get; set; } = [];

    /// <summary>
    /// 针对码号分类级别做的额外处理方式，此属性只影响输出结果，不影响码表读取。
    /// </summary>
    public CodeLevelBehavior LevelBehavior { get; set; }

    public override bool Join(object transmission)
    {
        CodeMapSettingsTransmission? forTransmission = 
            ReadTransmission(transmission, JsonContext.Default.CodeMapSettingsTransmission);

        if (forTransmission == null)
        {
            return false;
        }

        if (forTransmission.StartRow >= 1)
        {
            StartRow = forTransmission.StartRow;
        }

        if (forTransmission.CodeColumns.Length > 0)
        {
            CodeColumns = forTransmission.CodeColumns;
        }

        if (forTransmission.LabelColumns.Length > 0)
        {
            LabelColumns = forTransmission.LabelColumns;
        }

        if (Enum.TryParse<CodeLevelMarkType>(forTransmission.LevelMarkType, true, out var levelMarkType))
        {
            LevelMarkType = levelMarkType;
        }

        if (Enum.TryParse<CodeLevelOrderType>(forTransmission.OrderType, true, out var orderType))
        {
            OrderType = orderType;
        }

        if (Enum.TryParse<CodeLevelBehavior>(forTransmission.LevelBehavior, true, out var levelBehavior))
        {
            LevelBehavior = levelBehavior;
        }

        return true;
    }

    
    public override void Recive(object transimission)
    {
        CodeMapSettingsTransmission? forTransmission =
            ReadTransmission(transimission, JsonContext.Default.CodeMapSettingsTransmission);

        if (forTransmission == null)
        {
            return;
        }

        Name = forTransmission.Name;
        StartRow = forTransmission.StartRow;
        CodeColumns = forTransmission.CodeColumns;
        LabelColumns = forTransmission.LabelColumns;

        if (Enum.TryParse<CodeLevelMarkType>(forTransmission.LevelMarkType, true, out var levelMarkType))
        {
            LevelMarkType = levelMarkType;
        }

        if (Enum.TryParse<CodeLevelOrderType>(forTransmission.OrderType, true, out var orderType))
        {
            OrderType = orderType;
        }

        if (Enum.TryParse<CodeLevelBehavior>(forTransmission.LevelBehavior, true, out var levelBehavior))
        {
            LevelBehavior = levelBehavior;
        }
    }


    public override IExcelAddInTransmission Transmit(string? name)
    {
        CodeMapSettingsTransmission transmission = new()
        {
            Name = name ?? Name,
            StartRow = StartRow,
            CodeColumns = CodeColumns,
            LabelColumns = LabelColumns,
            LevelMarkType = LevelMarkType.ToString().ToUpper(),
            OrderType = OrderType.ToString().ToUpper(),
            LevelBehavior = LevelBehavior.ToString().ToUpper(),
            ColorLevels = ColorLevels
        };

        return transmission;
    }
}
